<template>
  <div class="app-container">
      <el-checkbox 
      v-model="showReviewer" 
      class="filter-item" 
      style="margin-left:15px;" 
    >
       <h4>是否显示审核人</h4> 
     </el-checkbox>
    <hr>

     <el-table
       v-loading="listLoading"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%;"
       >
       <el-table-column label="序号" align="center" width="65">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
       </el-table-column>
       
       <el-table-column label="日期" width="150px" align="center">
          <template slot-scope="scope">
             <span>{{ scope.row.display_time }}</span>
          </template> 
       </el-table-column>
       
        <el-table-column label="标题">
          <template slot-scope="scope">
            <span>{{ scope.row.title }}</span>
            <el-tag>{{ scope.row.type|typeFilter}}</el-tag>
          </template> 
       </el-table-column>  

      <el-table-column v-if="showReviewer" label="审核人" width="80px" align="center">
        <template slot-scope="scope">
          <span style="color:red;">{{ scope.row.reviewer }}</span>
        </template>
      </el-table-column>


       <el-table-column label="作者" width="150px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.author }}</span>
          </template> 
       </el-table-column> 

        <el-table-column label="重要性" width="80px" align="center">
          <template slot-scope="scope">
             <i v-for="n in +scope.row.importance"  class="el-icon-star-on"></i>
          </template> 
       </el-table-column>  
        
      <!-- 阅读数点击弹窗 -->
      <el-table-column label="阅读数" align="center" width="95">
        <template slot-scope="scope">
          <span v-if="scope.row.pageviews" style="color:red;cursor:pointer;"  @click="handleFetchPv(scope.row.pageviews)">{{ scope.row.pageviews }}</span>
          <span v-else>0</span>
        </template>
      </el-table-column>
   
      <el-table-column label="状态"  width="95">
        <template slot-scope="scope">
           <el-tag :type="scope.row.status | statusFilter">{{ scope.row.status }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作"  >
        <template slot-scope="scope">
          <el-button type="primary" size="mini"   @click="handleModifyStatus(scope.row,'draft')">编辑</el-button>
          <el-button v-if="scope.row.status!='published'" type="success" size="mini"  @click="handleModifyStatus(scope.row,'published')">发布</el-button>
          <el-button v-if="scope.row.status!='draft'" size="mini" @click="handleModifyStatus(scope.row,'draft')">草稿</el-button>
          <el-button v-if="scope.row.status!='deleted'" type="danger" size="mini" @click="handleModifyStatus(scope.row,'deleted')">删除</el-button>
        </template>
      </el-table-column>
     </el-table>
     <!-- 分页 -->
       <div style="margin-top:20px;">
          <el-pagination v-show="total>0" :current-page="listQuery.page"  :page-size="listQuery.limit" :total="total" background layout="total, prev, pager, next, jumper" 
            @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
      </div>
     <!-- 弹窗 -->
      <el-dialog :visible.sync="dialogPvVisible" title="阅读数统计">
        <el-table :data="pvData" border fit highlight-current-row style="width: 100%">
          <el-table-column prop="key" label="型号"/>
          <el-table-column prop="pv" label="数据"/>
        </el-table>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="dialogPvVisible = false" size="small">提交</el-button>
        </span>
      </el-dialog>
  </div> 
</template>

<script>
import { fetchList, fetchPv, createArticle, updateArticle } from '@/api/article'
// import { parseTime } from '@/utils'

//定义一组数组
const calendarTypeOptions = [
  { key: 'CN', display_name: 'China' },
  { key: 'US', display_name: 'USA' },
  { key: 'JP', display_name: 'Japan' },
  { key: 'EU', display_name: 'Eurozone' }
]
//将数组转为对象
const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name
  return acc
}, {})
// console.log(calendarTypeKeyValue)

export default {
  name:'index',
   /* 过滤器 */
  filters: {
    /* 状态 */
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
      // console.log(statusMap[status])
    },
    /*  标题el-tag处理*/
    typeFilter(type) {
      return calendarTypeKeyValue[type]
    }
  },
    data() {
      return {
       showReviewer:false,
       listLoading:true,
       list:null,
       total: null,
       // 分页
      listQuery: {
        page: 1,
        limit: 5,      
      },
      dialogPvVisible:false,
      pvData: []
      }
    },
    created(){
      this.getList() 
    },
    methods: {
    /*初始化列表 */
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        /* 处理日期数据 */
        // this.list.forEach(element => {
        //    this.timestamp= new Date(element.timestamp)
        // });
       

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1000)
      })
      .catch(err=>{
        console.log(err)
      })
    },
    /* 分页方法 */
    /* 当前是第多少页 */
    handleSizeChange(val){
       this.listQuery.limit=val
      //  console.log(val)
      this.getList()
    },
    //  每页显示的条数
    handleCurrentChange(val){
      this.listQuery.page = val
      this.getList()
    },
     /* 阅读数弹窗 */
     handleFetchPv(pv){
        fetchPv(pv).then(response => {      
        this.pvData = response.data.pvData
        this.dialogPvVisible = true
      })
     },
     /* 点击按钮方法 */
     handleModifyStatus(row,status){
       this.$message({
         type:'success',
         message:'操作成功'
       })
      //  console.log(row,status)
        row.status=status
     }
    }
  }
</script>


<style  rel="stylesheet/scss" lang="scss"  scoped>
</style>
